﻿
// Author:					Doan Quoc Tuan
// Created:					2013-5-22
// Last Modified:			2013-5-22
// 
// The use and distribution terms for this software are covered by the 
// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)  
// which can be found in the file CPL.TXT at the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by 
// the terms of this license.
//
// You must not remove this notice, or any other, from this software.

using System;
using System.IO;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Configuration;

namespace SMSPanasonic.Data
{

    public static class DBSmsMtReport
    {
        /// <summary>
        /// Gets the connection string for read.
        /// </summary>
        /// <returns></returns>
        private static string GetReadConnectionString()
        {
            return Properties.Settings.Default.ConnectionString;

        }

        /// <summary>
        /// Gets the connection string for write.
        /// </summary>
        /// <returns></returns>
        private static string GetWriteConnectionString()
        {

            return Properties.Settings.Default.ConnectionString;

        }


        /// <summary>
        /// Inserts a row in the SmsMtReport table. Returns rows affected count.
        /// </summary>
        /// <param name="mtGuid"> mtGuid </param>
        /// <param name="sentTime"> sentTime </param>
        /// <param name="status"> status </param>
        /// <param name="reportTime"> reportTime </param>
        /// <param name="reportStatus"> reportStatus </param>
        /// <returns>int</returns>
        public static int Create(
            SqlGuid mtGuid,
            SqlDateTime sentTime,
            SqlString status,
            SqlDateTime reportTime,
            SqlString reportStatus)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "SmsMtReport_Insert", 5);
            sph.DefineSqlParameter("@MtGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, mtGuid);
            sph.DefineSqlParameter("@SentTime", SqlDbType.DateTime, ParameterDirection.Input, sentTime);
            sph.DefineSqlParameter("@Status", SqlDbType.VarChar, 15, ParameterDirection.Input, status);
            sph.DefineSqlParameter("@ReportTime", SqlDbType.DateTime, ParameterDirection.Input, reportTime);
            sph.DefineSqlParameter("@ReportStatus", SqlDbType.VarChar, 15, ParameterDirection.Input, reportStatus);
            int rowsAffected = sph.ExecuteNonQuery();
            return rowsAffected;

        }


        /// <summary>
        /// Updates a row in the SmsMtReport table. Returns true if row updated.
        /// </summary>
        /// <param name="mtGuid"> mtGuid </param>
        /// <param name="sentTime"> sentTime </param>
        /// <param name="status"> status </param>
        /// <param name="reportTime"> reportTime </param>
        /// <param name="reportStatus"> reportStatus </param>
        /// <returns>bool</returns>
        public static bool Update(
            SqlGuid mtGuid,
            SqlDateTime sentTime,
            SqlString status,
            SqlDateTime reportTime,
            SqlString reportStatus)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "SmsMtReport_Update", 5);
            sph.DefineSqlParameter("@MtGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, mtGuid);
            sph.DefineSqlParameter("@SentTime", SqlDbType.DateTime, ParameterDirection.Input, sentTime);
            sph.DefineSqlParameter("@Status", SqlDbType.VarChar, 15, ParameterDirection.Input, status);
            sph.DefineSqlParameter("@ReportTime", SqlDbType.DateTime, ParameterDirection.Input, reportTime);
            sph.DefineSqlParameter("@ReportStatus", SqlDbType.VarChar, 15, ParameterDirection.Input, reportStatus);
            int rowsAffected = sph.ExecuteNonQuery();
            return (rowsAffected > 0);

        }

        /// <summary>
        /// Deletes a row from the SmsMtReport table. Returns true if row deleted.
        /// </summary>
        /// <param name="mtGuid"> mtGuid </param>
        /// <returns>bool</returns>
        public static bool Delete(
            SqlGuid mtGuid)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "SmsMtReport_Delete", 1);
            sph.DefineSqlParameter("@MtGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, mtGuid);
            int rowsAffected = sph.ExecuteNonQuery();
            return (rowsAffected > 0);

        }

        /// <summary>
        /// Gets an IDataReader with one row from the SmsMtReport table.
        /// </summary>
        /// <param name="mtGuid"> mtGuid </param>
        public static IDataReader GetOne(
            SqlGuid mtGuid)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "SmsMtReport_SelectOne", 1);
            sph.DefineSqlParameter("@MtGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, mtGuid);
            return sph.ExecuteReader();

        }

        /// <summary>
        /// Gets a count of rows in the SmsMtReport table.
        /// </summary>
        public static int GetCount(string sWhereCondition)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "SmsMtReport_GetCount", 1);
            sph.DefineSqlParameter("@WhereCondition", SqlDbType.NVarChar, ParameterDirection.Input, sWhereCondition);
            return Convert.ToInt32(sph.ExecuteScalar());

        }

        /// <summary>
        /// Gets an IDataReader with all rows in the SmsMtReport table.
        /// </summary>
        public static IDataReader GetAll()
        {

            return SqlHelper.ExecuteReader(
                GetReadConnectionString(),
                CommandType.StoredProcedure,
                "SmsMtReport_SelectAll",
                null);

        }

        /// <summary>
        /// Gets a page of data from the SmsMtReport table.
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalPages">total pages</param>
        public static IDataReader GetPage(
            string whereCondition,
            string orderBy,
            int pageNumber,
            int pageSize,
            out int totalPages)
        {
            totalPages = 1;
            int totalRows
                = GetCount(whereCondition);

            if (pageSize > 0) totalPages = totalRows / pageSize;

            if (totalRows <= pageSize)
            {
                totalPages = 1;
            }
            else
            {
                int remainder;
                Math.DivRem(totalRows, pageSize, out remainder);
                if (remainder > 0)
                {
                    totalPages += 1;
                }
            }

            SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "SmsMtReport_SelectPage", 4);
            sph.DefineSqlParameter("@PageNumber", SqlDbType.Int, ParameterDirection.Input, pageNumber);
            sph.DefineSqlParameter("@PageSize", SqlDbType.Int, ParameterDirection.Input, pageSize);
            sph.DefineSqlParameter("@WhereCondition", SqlDbType.NVarChar, ParameterDirection.Input, whereCondition);
            sph.DefineSqlParameter("@OrderBy", SqlDbType.NVarChar, ParameterDirection.Input, orderBy);

            return sph.ExecuteReader();

        }

    }

}


